package cn.candy.org.service.impl;

import cn.candy.basic.exception.BusinessExcpetion;
import cn.candy.basic.service.impl.BaseServiceImpl;
import cn.candy.org.domain.Employee;
import cn.candy.org.domain.Shop;
import cn.candy.org.mapper.EmployeeMapper;
import cn.candy.org.mapper.ShopMapper;
import cn.candy.org.service.IEmployeeService;
import cn.candy.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 店铺业务实现
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {

        //1.基本信息审核 - 当前店铺入驻过，就不能再入驻【需求：一个店铺管理员就只能开一个店】
        Employee employee = employeeMapper.findByAccount(shop.getAdmin());
        if(employee != null){
            throw new BusinessExcpetion("入驻失败，你已经入驻过，请直接登录，忘记密码请找回!!!");
        }

        //2.店铺入驻
        //2.1. 先保存employee信息 - 可以获取到自增长的id
        employeeService.add(shop.getAdmin());
        //shop对象中的admin对象的id  有值了

        //2.2. 再保存shop信息 - 在shop中绑定管理员id
        shopMapper.save(shop); //shop对象中的admin对象的id 有值

        //2.3. 最后在管理员中绑定店铺id【根据employee_id 去修改 shop_id】
        employeeMapper.updateShopId(shop);
    }
}
